<?php
/**
 * Validate.php		
 *
 * @package		
 * @Copyright	(c) All rights reserved
 * @Author		skyCrack <skyCrack@126.com>
 * @Version		$Id$
 */

require_once 'Validate/Interface.php';

class Validate implements Validate_Interface {
	private $_rulePath		=	'';
	protected $_messages	=	array();
	
	public function __construct($rulePath = 'form') {
		$this->_rulePath	=	$rulePath;
	}
	
	public function __call($method, $params) {
		if ( substr($method, 0, 3) == 'get' ) {
			$ruleIdentity	=	substr($method, 3);
			//require_once $this->_rulePath . '/' . $ruleIdentity;
			$rule			=	$$ruleIdentity;
			var_dump($params);
			//return call_user_method_array('_get', $this, $params);
		}
	}
	
	protected function _get($rule) {}
	
	public function filter($rules, $data = null) {
		if ( is_null($data) ) {
			$data		=	$_REQUEST;
		}
		$input	=	filter_var_array($data, $rules);
		
		foreach ( $rules as $var => $rule ) {
			if ( $input[$var] === false ) {
				if ( isset($rule['error']) ) {
					$this->_messages[]	=	$rule['error'];
				} else {
					$this->_messages[]	=	'Parameter ' . $var . ' error.';
				}
				
				return false;
			}
			
			if ( is_array($rule) ) {
				if ( is_null($input[$var]) && array_key_exists('default', $rule) ) {
					$input[$var]				=	$rule['default'];
				}
				
				if ( array_key_exists('alias', $rule) ) {
					$input[$rules['alias']]		=	$input[$var];
					unset($input[$var]);
				}
			}
		}
		
		return $input;
	}
	
	public function isValid($value) {}
	
	public function getMessages() {
		return $this->_messages;
	}
}
?>